package com.isnotlocus.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.isnotlocus.gulimail.product.entity.ProductAttrValueEntity;
import com.isnotlocus.gulimail.product.service.ProductAttrValueService;
import com.isnotlocus.gulimail.product.vo.AttrResponseVo;
import com.isnotlocus.gulimail.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.isnotlocus.gulimail.product.entity.AttrEntity;
import com.isnotlocus.gulimail.product.service.AttrService;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.R;


/**
 * 商品属性
 *
 * @author IsnotLocus
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 17:00:57
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 获取所有基础信息
     * @param map
     * @param categoryId
     * @return
     */
    // http://localhost:88/api/product/attr/base/list/0
    @GetMapping("/{attrType}/list/{categoryId}")
    public R selectAttrByBase(@RequestParam() Map<String, Object> map, @PathVariable("categoryId") Long categoryId,@PathVariable("attrType") String attrType) {
        PageUtils pageUtils = attrService.SelectAttrByPage(map, categoryId,attrType);
        return R.ok().put("page", pageUtils);

    }

    /**
     * 回显SPU信息
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> entities=productAttrValueService.baseAttrListForSpu(spuId);
        return  R.ok().put("data",entities);
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
}


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrResponseVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attr) {
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attr) {
        int i = attrService.UpdateByDetails(attr);
        if (i > 0) {
            System.out.println("修改成功!!!!");
            return R.ok("修改成功!!!!");
        } else {
            return R.error("修改失败，未知错误");
        }

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds, @RequestParam Map<String, Object> map) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
